package com.ckerchina.admin.ckadmin.controller.console;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ckerchina.admin.ckadmin.controller.dto.MgrResponseData;
import com.ckerchina.admin.ckadmin.dao.AgentUserDao;
import com.ckerchina.admin.ckadmin.dao.CustomerUserDao;
import com.ckerchina.admin.ckadmin.dao.MgrMenuDAO;
import com.ckerchina.admin.ckadmin.dao.MgrUserAndMenuDAO;
import com.ckerchina.admin.ckadmin.dao.MgrUserDAO;
import com.ckerchina.admin.ckadmin.dao.TeacherInfoDao;
import com.ckerchina.admin.ckadmin.domain.AgentUser;
import com.ckerchina.admin.ckadmin.domain.CustomerUser;
import com.ckerchina.admin.ckadmin.domain.TeacherInfo;
import com.ckerchina.admin.ckadmin.domain.mgr.MgrMenu;
import com.ckerchina.admin.ckadmin.domain.mgr.MgrUser;
import com.ckerchina.admin.ckadmin.domain.mgr.MgrUserAndMenu;
import com.ckerchina.admin.ckadmin.service.mgr.MgrUserService;
import com.ckerchina.admin.ckadmin.vo.AgentUserVO;
import com.ckerchina.admin.ckadmin.vo.ComboVO;

import cn.osworks.aos.core.asset.AOSJson;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.asset.WebCxt;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;

/**
 * 基本操作相关功能。
 */
@Controller
public class ConsoleController extends AbstractConsoleController {
	@Autowired
	private MgrUserDAO mgrUserDAO;
	@Autowired
	private MgrMenuDAO mgrMenuDAO;

	@Autowired
	private AgentUserDao agentUserDAO;
	
	@Autowired
	private TeacherInfoDao teacherInfoDao;
	
	@Autowired
	private CustomerUserDao customerUserDao;

	@Autowired
	private MgrUserAndMenuDAO mgrUserAndMenuDAO;
	@Autowired
	private MgrUserService mgrUserService;

	@RequestMapping(value = "/index")
	public String index(Model model, HttpServletRequest request) {
		request.setAttribute("username", getMgrUser().getName());
		request.setAttribute("menus", mgrUserAndMenuDAO.findMgrMenuByMuserId(getMgrUser().getId()));
		System.out.println("index OK!");
		return "index";
	}

 

	@RequestMapping("/findAgents")
	public @ResponseBody String list123() {
		List<AgentUser> list = agentUserDAO.findAllAgentUser();
		List<AgentUserVO> lst = new ArrayList<AgentUserVO>();

		for (int i = 0; i < list.size(); i++) {
			AgentUser user = list.get(i);
			AgentUserVO av = new AgentUserVO();
			av.setValue(String.valueOf(user.getId()));
			av.setDisplay(user.getAgentName());
			lst.add(av);
		}
	
        String json = JSONObject.toJSONString(lst);
		logger.info("json:{}", json);

		System.out.println("findAgents OK!");;
		return json;
	}
	
	@RequestMapping("/findCustomerCompany")
	public @ResponseBody String findCustomerCompany() {
		List<CustomerUser> list = customerUserDao.findAll();
		List<ComboVO> lst = new ArrayList<ComboVO>();

		for (int i = 0; i < list.size(); i++) {
			CustomerUser user = list.get(i);
			ComboVO av = new ComboVO();
			av.setValue(String.valueOf(user.getId()));
			av.setDisplay(user.getAgent_name());
			lst.add(av);
		}
	
        String json = JSONObject.toJSONString(lst);
		logger.info("json:{}", json);

		System.out.println("findAgents OK!");;
		return json;
	}
	
	@RequestMapping("/findTeacher")
	public @ResponseBody String findTeacher() {
		List<TeacherInfo> list = teacherInfoDao.findAll();
		List<ComboVO> lst = new ArrayList<ComboVO>();

		for (int i = 0; i < list.size(); i++) {
			TeacherInfo user = list.get(i);
			ComboVO av = new ComboVO();
			av.setValue(String.valueOf(user.getId()));
			av.setDisplay(user.getTeacher_name());
			lst.add(av);
		}
	
        String json = JSONObject.toJSONString(lst);
		logger.info("json:{}", json);

		System.out.println("findAgents OK!");;
		return json;
	}


	@RequestMapping("/findActor")
	public @ResponseBody String listCompany() {

		//// Dto inDto = Dtos.newDto(request);
		//// inDto.put("parent_id_", "0");
		//// List<Dto> list = sqlDao.list("Demo.listComboBoxData", inDto);
		//// WebCxt.write(response, AOSJson.toJson(list));
		//// System.out.println("listComboBoxData:"+AOSJson.toJson(list));

		System.out.println("findActor OK!");
		// ResponseData rd = new ResponseData();
		// return
		// "[{\"display\":\"移动\",\"value\":\"1\"},{\"display\":\"联想",\"value\":\"2\"},{\"display\":\"中铁\",\"value\":\"3\"},{\"display\":\"创客中国\",\"value\":\"4\"}]";
		return "[{\"display\":\"代理商\",\"value\":\"1\"},{\"display\":\"渠道\",\"value\":\"2\"},{\"display\":\"管理员\",\"value\":\"3\"},{\"display\":\"账务\",\"value\":\"4\"},{\"display\":\"客服\",\"value\":\"5\"}]";
	}

	// 生成问候信息

	/**
	 * 下拉选择框服务器端数据源
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "findAgents1")
	public void listComboBoxData(HttpServletRequest request, HttpServletResponse response) {

	}

	/**
	 * 查询MgrUser
	 */
	@RequestMapping("/findAllMgrUser")
	public @ResponseBody List<MgrUser> findAllMgrUser() {
		System.out.println("findAllMgrUser OK!");
		return mgrUserDAO.findAllMgrUser();
	}

	/**
	 * 创建或修改MgrUser
	 */
	@RequestMapping(value = "/mergeMgrUser", method = RequestMethod.POST)
	public @ResponseBody MgrResponseData mergeMgrUser(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "name") String name, @RequestParam(value = "realname") String realname,
			@RequestParam(value = "pwd") String pwd, @RequestParam(value = "actortype") String actortype,
			@RequestParam(value = "companyid") String companyid, @RequestParam(value = "email") String email, @RequestParam(value = "state") Integer state) {
		mgrUserService.mergeMgrUser(id, name, realname, pwd, email, actortype, companyid,state);
		return new MgrResponseData("操作成功");
	}

	/**
	 * 逻辑删除MgrUser
	 */
	@RequestMapping(value = "/delMgrUser", method = RequestMethod.POST)
	public @ResponseBody MgrResponseData delMgrUser(@RequestParam String id) {
		for (String lid : id.split(",")) {
			mgrUserService.delMgrUser(Long.parseLong(lid));
		}
		return new MgrResponseData("操作成功");
	}

	/**
	 * 查询全部系统菜单
	 */
	@RequestMapping("/findAllMgrMenu")
	public @ResponseBody List<MgrMenu> findAllMgrMenu() {
		return mgrMenuDAO.findAllMgrMenu();
	}

	/**
	 * 创建或修改系统菜单
	 */
	@RequestMapping(value = "/mergeMgrMenu", method = RequestMethod.POST)
	public @ResponseBody MgrResponseData createOrMergeMgrMenu(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "name") String name, @RequestParam(value = "url") String url,
			@RequestParam(value = "iconCls") String iconCls) {
		mgrUserService.mergeMgrMemu(id, name, url, iconCls);
		return new MgrResponseData("操作成功");
	}

	/**
	 * 删除系统菜单
	 */
	@RequestMapping(value = "/delMgrMenu", method = RequestMethod.POST)
	public @ResponseBody MgrResponseData delMgrMenu(@RequestParam String id) {
		for (String lid : id.split(",")) {
			mgrUserService.delMgrMenu(Long.parseLong(lid));
		}
		return new MgrResponseData("操作成功");
	}

	/**
	 * 查询一个MgrUser所拥有的系统菜单
	 */
	@RequestMapping(value = "/findMgrUserAndMenuByMuserId")
	public @ResponseBody List<MgrUserAndMenu> findMgrUserAndMenuByMuserId(@RequestParam Long id) {
		return mgrUserAndMenuDAO.findUserMenuByMuserId(id);
	}

	/**
	 * 更新MgrUserde所有的系统菜单状态。1：有，0：没有
	 */
	@RequestMapping(value = "/updateUserMenu", method = RequestMethod.POST)
	public @ResponseBody MgrResponseData updateUserMenu(@RequestParam(value = "userId") Long userId,
			@RequestParam(value = "menuId") Long menuId, @RequestParam(value = "type") Boolean type) {
		mgrUserService.updateUserMenu(userId, menuId, type ? 1 : 0);
		return new MgrResponseData("操作成功");
	}

}
